package com.crm.service.impl.sys;

import com.crm.dao.mapper.customMapper.sys.UserInfoCustomMapper;
import com.crm.dao.mapper.sys.UserInfoMapper;
import com.crm.dao.mapper.sys.UserInfoMapper;
import com.crm.model.entity.sys.UserInfo;
import com.crm.model.entity.sys.UserInfo;
import com.crm.model.entity.sys.UserInfoExample;
import com.crm.model.vo.user.UserInfoVO;
import com.crm.service.sys.UserInfoDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author luojie
 * @date 2020/10/9 9:35
 */
@Service
public class UserInfoDbServiceImpl implements UserInfoDbService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserInfoCustomMapper userInfoCustomMapper;

    @Override
    public UserInfo selectUserInfoByUserId(String userId) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
        return userInfo;
    }

    @Override
    public Boolean insertUserInfo(UserInfo userInfo) {
        int num = userInfoMapper.insertSelective(userInfo);
        return num>0;
    }

    @Override
    public Boolean updateUserInfoByUserId(UserInfo userInfo) {
        int num = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        return num>0;
    }

    /**
     * 通过userId查询个人信息
     * @param userId 用户id
     * @return
     */
    @Override
    public UserInfoVO getUserInfo(String userId) {
        return userInfoCustomMapper.getUserInfo(userId);
    }

    /**
     * 根据用户id查询用户的下属
     * @param order 分组
     * @param userId 用户id
     * @param onTheJobSituation 在职状态  0在职  1离职
     * @return
     */
    @Override
    public UserInfo subordinateList(String order, String userId,String onTheJobSituation) {
        UserInfoExample userInfoExample = new UserInfoExample();
        //userInfoExample.setOrderByClause(order);
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        if(!StringUtils.isBlank(onTheJobSituation)) {
            criteria.andOnTheJobSituationEqualTo(Integer.parseInt(onTheJobSituation));
        }
        List<UserInfo> list = userInfoMapper.selectByExample(userInfoExample);
        if(list.size()!= 0){
            return list.get(0);
        }else{
            return new UserInfo();
        }

    }

    /**
     * 根据账号表id查询用户信息
     * @param accountId
     * @return
     */
    @Override
    public UserInfoVO getUserInfoByAccountId(String accountId) {
        return userInfoCustomMapper.getUserInfoByAccountId(accountId);
    }


    @Override
    public UserInfo getUserInfoByUserNameAndCompanyName(String userName, String companyName) {
        UserInfoExample userInfoExample = new UserInfoExample();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        criteria.andCompanyNameEqualTo(companyName);
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(userInfoExample);
        if(userInfoList.size()>0){
            return userInfoList.get(0);
        }else {
            return null;
        }
    }

    /**
     * 通过accountId查询
     * @param accountId
     * @return
     */
    @Override
    public UserInfoVO selectByAccountId(String accountId) {
        return userInfoCustomMapper.selectByAccountId(accountId);
    }
}
